package pl.gigiel.behaviour.util;

import com.jme.math.Vector3f;

public class Regular2DPositionGenerator {
	private static Vector3f currentPosition = new Vector3f();
	private static Vector3f currentDirection = new Vector3f(Vector3f.UNIT_Y);
	private static int step = 0;
	private static int stepPhase = 0;
	private static int stepSize = 1;

	public static Vector3f position(Vector3f size, Vector3f cornerMin, Vector3f cornerMax) {
		Vector3f result = null;
		if (++step > stepSize) {
			changeCurrentDirection();
			if (++stepPhase % 2 == 0) {
				stepSize++;
				stepPhase = 0;
			}
			step = 1;
		}
		calculateCurrentPosition(size);

		result = new Vector3f(currentPosition);
		return result;
	}

	private static void calculateCurrentPosition(Vector3f size) {
		if (currentDirection.equals(Vector3f.UNIT_Y)) {
			currentPosition.setY(currentPosition.getY() + size.getY());
		} else if (currentDirection.equals(Vector3f.UNIT_Y.negate())) {
			currentPosition.setY(currentPosition.getY() - size.getY());
		} else if (currentDirection.equals(Vector3f.UNIT_X)) {
			currentPosition.setX(currentPosition.getX() + size.getX());
		} else if (currentDirection.equals(Vector3f.UNIT_X.negate())) {
			currentPosition.setX(currentPosition.getX() - size.getX());
		}
	}

	private static void changeCurrentDirection() {
		if (currentDirection.equals(Vector3f.UNIT_Y)) {
			currentDirection = new Vector3f(Vector3f.UNIT_X);
		} else if (currentDirection.equals(Vector3f.UNIT_Y.negate())) {
			currentDirection = new Vector3f(Vector3f.UNIT_X.negate());
		} else if (currentDirection.equals(Vector3f.UNIT_X)) {
			currentDirection = new Vector3f(Vector3f.UNIT_Y.negate());
		} else if (currentDirection.equals(Vector3f.UNIT_X.negate())) {
			currentDirection = new Vector3f(Vector3f.UNIT_Y);
		}
	}
}
